package com.fbee.smartcommunities.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fbee.smartcommunities.entity.User;
import com.fbee.smartcommunities.mapper.UserMapper;
import com.fbee.smartcommunities.service.UserService;
import com.fbee.smartcommunities.vo.LoginVO;
import com.fbee.smartcommunities.vo.ResultVO;
import io.minio.errors.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.security.reactive.ReactiveUserDetailsServiceAutoConfiguration;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.mail.MessagingException;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.time.LocalDate;
import java.util.List;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author 汪佳泉
 * @since 2024-08-16
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @GetMapping("/all")
    public ResultVO<List<User>> getAll(){
        return ResultVO.success(userService.list());
    }

    // 分页后内容
    @GetMapping("/list")
    public ResultVO<IPage<User>> getPage(Integer pageNo, Integer pageSize) {
        return ResultVO.success(userService.page(new Page<>(pageNo,pageSize)),null);
    }

    // 添加用户
    @PostMapping("/add")
    public ResultVO<Boolean> add(String name,
                                      String nickname,
                                      LocalDate birthday,
                                      String phone,
                                      String email,
                                      String address,
                                      String password,
                                      Integer state,
                                      Integer roleId,
                                      MultipartFile file) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(userService.add(name, nickname, birthday, phone, email,address, password, state,roleId,file),"添加成功");
    }

    @PostMapping("/update")
    public ResultVO<Boolean> upDate(Integer id,
                                         String name,
                                         String nickName,
                                         LocalDate birthday,
                                         String phone,
                                         String email,
                                         String address,
                                         MultipartFile file,
                                         String icon
                                         ) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(userService.update(id,name,nickName,birthday,phone,email,address,file,icon),"修改成功");
    }

    @PostMapping("/completeInformation")
    public ResultVO<String> completeInformation(Integer openId,
                                                 String name,
                                                 String nickName,
                                                 LocalDate birthday,
                                                 String phone,
                                                 String address,
                                                 MultipartFile file,
                                                 String icon ) throws ServerException, InsufficientDataException, ErrorResponseException, IOException, NoSuchAlgorithmException, InvalidKeyException, InvalidResponseException, XmlParserException, InternalException {
        return ResultVO.success(userService.completeInformation(openId,name,nickName,birthday,phone,address,file,icon),"个人信息修改成功");
    }

    // 获取一个用户，用于表单回显
    @GetMapping("/getById")
    public ResultVO<User> getById(Long id){
        return ResultVO.success(userService.getById(id),null);
    }

    // 获取在线用户
    @GetMapping("/getNowUser")
    public ResultVO<User> getUserById(Long openId){
        return ResultVO.success(userService.getById(openId),null);
    }

    // 修改管理员状态
    @PostMapping("/fixState")
    public ResultVO<Boolean> fixState(Long id, Boolean state){
        return ResultVO.success(userService.fixState(id,state),"修改成功");
    }

    @GetMapping("/find")
    public ResultVO<IPage<User>> getTargetList(Integer pageNo, Integer pageSize, String keyword, Integer roleId){
        return ResultVO.success(userService.getTargetList(pageNo,pageSize,keyword,roleId),null);
    }

    /**
     * 检查某个字段是否有重复的
     * @param column
     * @param value
     * @param id
     * @return false-说明存在 true-说明不存在
     */
    @GetMapping("/check")
    public ResultVO<Boolean> check(String column, String value, Integer id){
        return ResultVO.success(userService.check(column,value,id));
    }

    @GetMapping("/checkUser")
    public ResultVO<Boolean> checkUser(String column, String value, Integer openId){
        return ResultVO.success(userService.check(column,value,openId));
    }

    @PostMapping("/login")
    public ResultVO<LoginVO> login(String username, String password,String type,HttpServletRequest request){
        return ResultVO.success(userService.login(username,password,type,request));
    }

    @PostMapping("/sendemail")
    public ResultVO<Boolean> sendEmail(String to,String state) throws MessagingException {
        return ResultVO.success(userService.getCaptcha(to,state),"验证码已经成功发送至您的邮箱,请注意查看(10分钟内有效)");
    }

    @PostMapping("/fixPassword")
    public ResultVO<Boolean> fixPassword(String username,String password,String code){
        return ResultVO.success(userService.fixPassword(username,password,code),"修改成功，请前往登入");
    }

    @GetMapping("/getByTitle")
    public ResultVO<List<User>> getUsersByNoticeTitle(String title){
        return ResultVO.success(userService.getUsersByNoticeTitle(title));
    }

    @PostMapping("/registration")
    public ResultVO<Boolean> userRegistration(String email,String password,String code,Integer role){
        return ResultVO.success(userService.registration(email,password,code,role),"注册成功，请前往登入");
    }

    @GetMapping("/checkNull")
    public ResultVO<Boolean> checkNull(Long openId){
        return ResultVO.success(userService.checkNull(openId));
    }

    @PostMapping("/recharge")
    public ResultVO<Boolean> recharge(Double count,Long openId,HttpServletRequest request){
        return ResultVO.success(userService.recharge(count,openId,request),"充值成功！");
    }

}
